package com.zgxt.demo.controller;



import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zgxt.demo.Dao.UserDao;
import com.zgxt.demo.entity.DialogueDetails;
import com.zgxt.demo.service.DialogueDetailsService;
import com.zgxt.demo.service.UserService;
import com.zgxt.demo.util.BigModelNewV4_0Ultra;
import com.zgxt.demo.util.UserHolder;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class DialogueDetailsController {


    @Resource
    private DialogueDetailsService dialogueDetailsService;



    @RequestMapping("selectSessionRecord")
    public List<DialogueDetails> selectSessionRecord(@RequestParam Integer currentPage,@RequestParam Integer rows){
        UserDao userDao= UserHolder.getUserDao();
        return dialogueDetailsService.selectSessionRecord(userDao.getUserId(),currentPage,rows);
    }


    @RequestMapping("Dialogue")
    public DialogueDetails DialogueAI(@RequestParam String cent) throws Exception {
        UserDao userDao= UserHolder.getUserDao();
        String userId = Integer.toString(userDao.getUserId());
        dialogueDetailsService.sessionRecord(cent,userId,1);
        BigModelNewV4_0Ultra bigModelNewV40Ultra=new BigModelNewV4_0Ultra(userId,true);
        bigModelNewV40Ultra.ai(cent);
        return selectSessionRecord(userId);
    }

    @GetMapping("/deleteChat")
    public String deleteChat(){
        UserDao userDao= UserHolder.getUserDao();
        JSONObject _out=new JSONObject();
        if (dialogueDetailsService.deleteChat(userDao.getUserId())>0){
            _out.put("code",200);
            _out.put("msg","删除成功");
        }else {
            _out.put("code",500);
            _out.put("msg","删除失败");
        }
        return _out.toJSONString();
    }

    @PostMapping("/queryDialogue")
    public Map<String,Object> QueryChatHistory(@RequestParam Integer page, @RequestParam Integer limit, String name){
        return dialogueDetailsService.QueryChatHistory(limit*page,limit,name);
    }

    @PostMapping("/deleteDialogue")
    public String deleteDialogue(@RequestParam String ids){
        JSONObject _out=new JSONObject();
        if (dialogueDetailsService.DisableChatHistory(ids)>0){
            _out.put("code",200);
            _out.put("msg","删除成功");
        }else {
            _out.put("code",500);
            _out.put("msg","删除失败");
        }
        return _out.toJSONString();
    }

    //轮询会话记录
    private DialogueDetails selectSessionRecord(String titleId) throws InterruptedException {
         List<DialogueDetails> dialogueDetails=dialogueDetailsService.selectSessionRecord(Integer.valueOf(titleId),0,1);
         List<DialogueDetails> detailsList;
         int i=0;
         while (true){
             detailsList=dialogueDetailsService.selectSessionRecord(Integer.valueOf(titleId),0,1);
             System.out.println("轮询:"+i++);
             if (dialogueDetails.get(0).getId()<detailsList.get(0).getId()){
                 break;
             }
             //这句时会等待500毫秒再执行
             TimeUnit.MILLISECONDS.sleep(500);//单位是毫秒
         }
        return detailsList.get(0);
    }

}
